package Controls;

import Core.*;
import javax.microedition.lcdui.Graphics;

public class Slider extends Control
{
	public static String StandartInactivePath="/strong-dark.cfg";
	public static String StandartActivePath="/strong-light.cfg";
	private double position=0;
	private double step=0.03;
	private Img aline, iline, amover, imover;

	private int moverH = 16, moverW = 4;
	private int lineH = 10;
	private int border = 4;

	public Slider(int w)
	{
		aline = GUICanvas.pool.CreateImage(StandartActivePath, w, lineH, Form.StandartBGColor, false);
		iline = GUICanvas.pool.CreateImage(StandartInactivePath, w, lineH, Form.StandartBGColor, false);

		amover = GUICanvas.pool.CreateImage(StandartActivePath, moverW, moverH, Form.StandartBGColor, false);
		imover = GUICanvas.pool.CreateImage(StandartInactivePath, moverW, moverH, Form.StandartBGColor, false);

		W = w;
		H = moverH;

		lockOn = true;
		selectedAction = "Ред.";
		pressedAction = "Назад";
	}

	public void draw(Graphics g, int x, int y)
	{
		Img am, al;

		if(state == Control.INACTIVE)
		{
			am = imover;
			al = iline;
		}
		else if(state == Control.ACTIVE)
		{
			am = imover;
			al = aline;
		}
		else
		{
			am = amover;
			al = aline;
		}

		al.draw(g, x, y + (H - lineH)/2);
		am.draw(g, x + border + (int)((W-border*2)*position), y, Graphics.TOP | Graphics.HCENTER);
	}

	public void processKey(int key, int state)
	{
		super.processKey(key, state);
		
		if(state == Key.PRESSED || state == Key.REPEATED)
		{
			if(key == Key.PL_KEY || key == '4') decrement();
			if(key == Key.PR_KEY || key == '6') increment();

			if(parentForm != null) parentForm.onEvent(GM.SLIDER_MOVED, this);
		}
	}

	public void increment()
	{
		position += step;
		if(position > 1) position=1;
	}

	public void decrement()
	{
		position -= step;
		if(position < 0) position=0;
	}

	public void setPosition(double position)
	{
		if(position>=0 && position<=1) this.position = position;
	}

	public double getPosition()
	{
		return position;
	}

	public void setStep(double step)
	{
		if(step>=0 && step<=1) this.step = step;
	}

	public double getStep()
	{
		return step;
	}

}
